package game.players.goalkeeper.mate;

import game.ball.SoccerBall;
import game.players.SoccerPlayer;
import game.players.goalkeeper.FlyingToTheNorth;
import game.players.goalkeeper.FlyingToTheSouth;
import game.players.goalkeeper.Goalkeeper;
import game.players.goalkeeper.GoalkeeperState;

import com.uqbar.vainilla.DeltaState;

public class GoalkeeperWithoutBallMate extends GoalkeeperState{
	
	private int timer = 2;//para que no colisione inmediatamente despues de dar el pase

	public GoalkeeperWithoutBallMate(SoccerPlayer goalkeeper) {
		super(goalkeeper);
		goalkeeper.setAppearance(((Goalkeeper) goalkeeper).getStationarySprite());
	}

	@Override
	public void update(DeltaState deltaState) {
		if(!this.getGoalkeeper().isAccommodated()){
			this.getGoalkeeper().setState(new ReturningToTheArcMateGoalkeeper(this.getGoalkeeper()));
		}else{
			if(this.timer==0){
				if(this.collidesAgainstBall())
					this.getGoalkeeper().takeTheBall();
				else
					this.tackleTheBall();	
			}else
				this.timer--;
		}
	}
	

	
	public void tackleTheBall() {
		SoccerBall ball = this.getGoalkeeper().getScene().getSoccerBall();
		if(IHaveToAct(ball)){
			//si la pelota viene por el norte
			if(ball.getY() < this.getGoalkeeper().getY()-ball.getAppearance().getHeight())
				this.flyToTheNorth();				
			else{//la pelota viene por el sur
				if(ball.getY() > this.getGoalkeeper().getY()+this.getGoalkeeper().getAppearance().getHeight())
					this.flyToTheSouth();
				else//la pelota viene por el medio
					this.walkToTheBall();
			}		
		}	
	}

	public void walkToTheBall() {
		this.getGoalkeeper().setState(new WalkingToTheBallMateGoalkeeper(this.getGoalkeeper()));		
	}

	public void flyToTheSouth() {
		this.getGoalkeeper().setState(new FlyingToTheSouth(this.getGoalkeeper()));
	}

	public void flyToTheNorth() {
		this.getGoalkeeper().setState(new FlyingToTheNorth(this.getGoalkeeper()));		
	}

	public boolean IHaveToAct(SoccerBall ball) {		
		return ball.getX()<this.getGoalkeeper().getX()+200 && ball.isWithinField();
	}

	@Override
	public boolean isWithBall() {
		return false;
	}

	@Override
	public boolean isRight() {
		return true;
	}
	
}
